package com.cskaoyan.service.impl;

import com.cskaoyan.bean.bo.LoginBO;
import com.cskaoyan.bean.pojo.MallAdmin;
import com.cskaoyan.bean.pojo.MallAdminExample;
import com.cskaoyan.bean.pojo.MallRole;
import com.cskaoyan.bean.vo.AuthInfoVO;
import com.cskaoyan.bean.vo.PermissionVO;
import com.cskaoyan.mapper.MallAdminMapper;
import com.cskaoyan.mapper.MallRoleMapper;
import com.cskaoyan.mapper.MallRolePermissionMapper;
import com.cskaoyan.service.AuthService;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: zoey-zhu
 * @description: 首页(GET)：查询订单、用户、货品、订单的数量
 * @Date: 20:37 2021/7/8
 */
@Service
public class AuthServiceImlp implements AuthService {

    @Autowired
    MallAdminMapper mallAdminMapper;

    @Autowired
    MallRolePermissionMapper mallRolePermissionMapper;

    @Autowired
    MallRoleMapper mallRoleMapper;

    /**
     * @Author: zoey-zhu
     * @description: login（POST）: 登录
     * @Date 22:52 2021/7/8
     * url: http://182.92.235.201:8083/admin/auth/login
     */
    @Override
    public MallAdmin login(LoginBO loginBO) {

        MallAdminExample mallAdminExample = new MallAdminExample();
        MallAdminExample.Criteria criteria = mallAdminExample.createCriteria();
        criteria.andUsernameEqualTo(loginBO.getUsername());
        criteria.andPasswordEqualTo(loginBO.getPassword());

        List<MallAdmin> mallAdmins = mallAdminMapper.selectByExample(mallAdminExample);

        int size = mallAdmins.size();

        if (size == 1) {
            return mallAdmins.get(0);
        } else {
            return null;
        }
    }

    /**
     * @Author: zoey-zhu
     * @description: info（GRT）: 登录后返回的信息
     * @Date 21:35 2021/7/8
     * url: http://182.92.235.201:8083/admin/auth/info?token=39dfa3d4-fed5-475f-b527-4361fb2a6d14
     */
    @Override
    public AuthInfoVO info(MallAdmin admin) {

        Integer[] roleIds = admin.getRoleIds();
        List<String> permissions = new ArrayList<>();
        List<String> roles = new ArrayList<>();
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        for (Integer roleId : roleIds) {
            if (roleId.equals(1)) {
                permissions.add("*");
                break;
            }
            MallRole mallRole = mallRoleMapper.selectByPrimaryKey(roleId);
            roles.add(mallRole.getName());
            PermissionVO permissionVO = mallRolePermissionMapper.selectRolePermissionVOByRoleId(roleId);
            String[] permissions1 = permissionVO.getPermissions();
            for (String s : permissions1) {
                if (s.equals("POST /admin/storage/read")) {
                    permissions.add(s);
                    continue;
                }
                if (s.equals("GET /admin/index/read")) {
                    permissions.add(s);
                    continue;
                }
                if (s.equals("admin:order:ship")) {
                    permissions.add("POST /admin/order/ship");
                    continue;
                }
                if (s.equals("admin:order:refund")) {
                    permissions.add("POST /admin/order/refund");
                    continue;
                }
                if (s.equals("admin:order:reply")) {
                    permissions.add("POST /admin/order/reply");
                    continue;
                }
                if (s.equals("GET /admin/groupon/listRecord")) {
                    permissions.add(s);
                    continue;
                }
                if (s.contains("list") || s.contains("read")) {
                    String replace = s.replace(":", "/");
                    replace = "GET /" + replace;
                    permissions.add(replace);
                } else if (s.contains("update") || s.contains("create") || s.contains("delete")) {
                    String replace = s.replace(":", "/");
                    replace = "POST /" + replace;
                    permissions.add(replace);
                } else {
                    permissions.add(s);
                }
            }
        }
        if (permissions.contains("*")) {
            List<String> permissions2 = new ArrayList<>();
            permissions2.add("*");
            return new AuthInfoVO(admin.getUsername(), admin.getAvatar(), roles, permissions2);
        } else {
            return new AuthInfoVO(admin.getUsername(), admin.getAvatar(), roles, permissions);
        }
    }
}
